package test;

import org.junit.Test;

import java.util.Arrays;

/**
 * @Author Sven
 * To be the best
 */
public class TryQuickSort {


    private Integer quickSort(int[] array, int i, int j) {
        int base = array[i];
        while (i < j) {
            //sort the array from the start to array[i-1]
            while (i < j && array[j] >= base) {
                j--;
            }
            if (i < j) {
                array[i] = array[j];
            }

            //sort the array from  the right to array[i+1] to the end
            while (i < j && array[i] <= array[j]) {
                i++;
            }
            if (i < j) {
                array[j] = array[i];
            }
        }
        array[i] = base;
        return i;
    }

    //recurcive call
    public void sort(int[] array, int i, int j) {
        if (i < j) {
            Integer flag = quickSort(array, i, j);
            sort(array, i, flag - 1);
            sort(array, flag + 1, j);
        }
    }

    //Override Sort method
    public void sort(int[] array) {
        sort(array, 0, array.length - 1);
    }

    @Test
    public void Entrance() {
        int[] array = new int[]{48, 6, 88, 60, 42, 83, 73, 88, 85};
        this.sort(array);

        System.out.println(Arrays.toString(array));
    }

}
